package com.xydtech.common.utils.sql;

import com.alibaba.druid.filter.config.ConfigTools;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 创建时间：2021/11/17 17:20
 * 项目名称：xydtech
 * @author JHON
 * @version 1.0
 * @since JDK 1.8.0_201
 * 文件名称：RsaEncryptAndDecodeUtil
 * 类说明：使用com.alibaba.druid.filter.config.ConfigTools进行数据库密码加密和解密工具类
 */

@Component
public class RsaEncryptAndDecodeUtil {

    /**
     * 公钥(用于解密)
     */
    private static String RSA_PUBLIC_KEY;

    /**
     * 私钥加密(用于加密)
     */
    private static String RSA_PRIVATE_KEY;

    @Value("${rsaenc.publickey}")
    public void setPublickey(String publickey) {
        RsaEncryptAndDecodeUtil.RSA_PUBLIC_KEY = publickey;
    }

    @Value("${rsaenc.privatekey}")
    public void setPrivatekey(String privatekey) {
        RsaEncryptAndDecodeUtil.RSA_PRIVATE_KEY = privatekey;
    }

    public static void main(String[] args) {
        String miwen = encrypt("");
        String a = RSA_PRIVATE_KEY;
        System.out.println("密文=====>" + miwen);
        String mingwen = decrypt(miwen);
        System.out.println("名文=====>" + mingwen);
    }

    /**
     * 描述 私钥加密
     *
     */
    public static String encrypt(String strMing) {
        try {
            return ConfigTools.encrypt(RSA_PRIVATE_KEY, strMing);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 描述 公钥解密
     *
     */
    public static String decrypt(String strMi) {
        try {
            return ConfigTools.decrypt(RSA_PUBLIC_KEY, strMi);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
